﻿using Azure;
using CKS.Core.Framework;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

//创建时间：2025-10-04
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 数据库采集配置表
    /// </summary>
    [Verify]
    [Route("business/DatabaseCollectionConfig")]
    [ApiExplorerSettings(GroupName = "business")]
    public class DatabaseCollectionConfigController : BaseController
    {
        /// <summary>
        /// 数据库采集配置表接口
        /// </summary>
        private readonly IDatabaseCollectionConfigService _DatabaseCollectionConfigService;

        public DatabaseCollectionConfigController(IDatabaseCollectionConfigService DatabaseCollectionConfigService)
        {
            _DatabaseCollectionConfigService = DatabaseCollectionConfigService;
        }

        /// <summary>
        /// 查询数据库采集配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:list")]
        public IActionResult QueryDatabaseCollectionConfig([FromQuery] DatabaseCollectionConfigQueryDto parm)
        {
            var response = _DatabaseCollectionConfigService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询数据库采集配置表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:query")]
        public IActionResult GetDatabaseCollectionConfig(int Id)
        {
            var response = _DatabaseCollectionConfigService.GetInfo(Id);

            var info = response.Adapt<DatabaseCollectionConfigDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加数据库采集配置表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:add")]
        [Log(Title = "数据库采集配置表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddDatabaseCollectionConfig([FromBody] DatabaseCollectionConfigDto parm)
        {
            var modal = parm.Adapt<DatabaseCollectionConfig>().ToCreate(HttpContext);

            var response = _DatabaseCollectionConfigService.AddDatabaseCollectionConfig(modal);

            return SUCCESS(response);
        }

        [HttpPost("SaveTableSyncConfig")]
        public IActionResult SaveTableSyncConfig([FromBody]DatabaseCollectionTables parm)
        {
            fsql.InsertOrUpdate<DatabaseCollectionTables>().SetSource(parm).ExecuteAffrows();
            return SUCCESS(parm);
        }

        [HttpDelete("DeleteTableSyncConfig/{ids}")]
        public IActionResult DeleteTableSyncConfig([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);
            var res = fsql.Delete<DatabaseCollectionTables>().Where(a=>a.Id.In(idArr)).ExecuteAffrows();
            return SUCCESS(res);
        }

        [HttpGet("QueryTableSyncConfig")]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:list")]
        public IActionResult QueryTableSyncConfig([FromQuery] DatabaseCollectionConfigQueryDto parm)
        {
            var page = new PagedInfo<DatabaseCollectionTables>();
            var lst = fsql.Select<DatabaseCollectionTables>().Page(parm.PageNum, parm.PageSize)
                .WhereIf(!parm.BusinessCode.IsEmpty(),a=>a.BusinessCode == parm.BusinessCode)
                .Count(out var total)
                .ToList();
            page.TotalNum = total.ToInt();
            page.PageSize = parm.PageSize;
            page.PageIndex = parm.PageNum;
            page.Result = lst;
            return SUCCESS(page);
        }



        /// <summary>
        /// 更新数据库采集配置表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:edit")]
        [Log(Title = "数据库采集配置表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateDatabaseCollectionConfig([FromBody] DatabaseCollectionConfigDto parm)
        {
            var modal = parm.Adapt<DatabaseCollectionConfig>().ToUpdate(HttpContext);
            var response = _DatabaseCollectionConfigService.UpdateDatabaseCollectionConfig(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除数据库采集配置表
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "databasecollectionconfig:delete")]
        [Log(Title = "数据库采集配置表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteDatabaseCollectionConfig([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_DatabaseCollectionConfigService.Delete(idArr));
        }

    }
}